<?php

require_once("./cfg/settings.php");
require_once("./cfg/admin.php");

class PluginBase
{
	protected $msgtype;
	protected $time;
	protected $users;

	//Write a message to the server
	function give($name, $id, $qty)
	{
		while($qty > 64)
		{
			echo "give $name $id 64\n";
			usleep(100000);
			$qty -= 64;
		}
		if($qty > 0)
			echo "give $name $id $qty\n";
	}

	function isAdmin($name)
	{
		global $admins;
		return ($name == "SMP Server" || in_array($name, $admins));
	}

	function writeMessage($msg)
	{
		echo $msg . "\n";
	}

        function processRawMessage($time, $msg)
        {
        }

        function processChatCommand($from, $cmd, $args)
        {
        }

        function processChatMessage($from, $message)
        {
        }
	
	function processUserJoin($user, $ip)
	{
	}

	function processUserPart($user, $reason)
	{
	}

	function messageType()
	{
		return $this->msgtype;
	}

	function messageTime()
	{
		global $DATEFORMAT;
		return ($this->time == "Invalid" ? date($DATEFORMAT) : $this->time);
	}

	//Call this function with a message for the plugin to process
	function processMessage($line)
	{
		global $DATEFORMAT;
		$this->time = date($DATEFORMAT);
		$this->msgtype = "Other";
		$msg = "";
		if (preg_match("/^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}) (.*)$/", $line, $matches) > 0)
		{
			// fwrite(STDERR, strtotime($matches[1]) . "\n");
			$this->time = date($DATEFORMAT, strtotime($matches[1]));
			$msg = $matches[2];
		}
		else
		{
			$this->time = date($DATEFORMAT);
			$msg = $line;
		}
		$this->processRawMessage($this->time, $msg);
                if ($line[0] == "!" || (preg_match("/^\\[INFO\\] [^ ]*<([^>]*)> (.*)$/", $msg, $matches) > 0))
                {
			$this->msgtype = "Chat";
                        usleep(50000);
                        $admin = false;
                        if ($line[0] == "!")
                        {
				$this->msgtype = "Server";
                                $line = trim($line);
                                $matches[1] = "SMP Server";
                                $matches[2] = $line;
                        }
                        $args = explode(' ', $matches[2]);
                        $cmd = $args[0];
                        unset($args[0]);
			$real_args = array();
			foreach($args as $arg)
				$real_args[] = $arg;
			$this->processChatCommand($matches[1], $cmd, $real_args);
			$this->processChatMessage($matches[1], $matches[2]);
		}
		else if (preg_match("/^\\[INFO\\] ([^ ]*) \\[\\/(.*)\\] logged in$/", $msg, $matches) > 0)
		{
			$this->msgtype = "Join";
			$this->processUserJoin($matches[1], $matches[2]);
//			fwrite(STDERR, "
		}
		// 2010-08-06 17:42:50 [INFO] ShadowGene lost connection: Quitting
		else if (preg_match("/^\\[INFO\\] ([^ ]*) lost connection: (.*)$/", $msg, $matches) > 0)
		{
			$this->msgtype = "Part";
			$this->processUserPart($matches[1], $matches[2]);
		}
		$this->msgtype = "Invalid";
		$this->time = "Invalid";
	}
}

function registerPlugin($name, PluginBase $plugin)
{
	global $SMP_Plugins;
	if(isset($SMP_Plugins[$name]))
	{
		fwrite(STDERR, "WARNING: Plugin attempted to register twice, " . $name . ", ignoring second definition.\n");
		return false;
	}
	$SMP_Plugins[$name] = $plugin;
	return true;
}


?>
